package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.brand.DashBrand;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.common.User;
import com.cskaoyan.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author stone
 * @date 2022/06/25 11:05
 */
@RestController
@RequestMapping("admin")
public class AdminUserController {

    @Autowired
    UserService userService;

    @RequestMapping("user/list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo list(MarketUser marketUser, BasePageInfo info) {
        CommonData<MarketUser> data = userService.query(marketUser, info);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("user/update")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo update(@RequestBody MarketUser marketUser) {
        int data = userService.update(marketUser);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("user/detail")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo detailList(String id) {
        MarketUser marketUser = new MarketUser();
        if (StringUtils.isNumeric(id) && !StringUtils.isEmpty(id)) {
            marketUser.setId(Integer.parseInt(id));
        } else {
            marketUser.setId(0);
        }

        MarketUser data = userService.queryById(marketUser);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("dashboard")
    public BaseRespVo dashboard() {
        DashBrand dashBrand = userService.queryTotal();
        BaseRespVo ok = BaseRespVo.ok(dashBrand);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("address/list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo addressList(String userId, String name, BasePageInfo info) {
        MarketAddress marketAddress = new MarketAddress();
        if (StringUtils.isNumeric(userId) && !StringUtils.isEmpty(userId)) {
            marketAddress.setUserId(Integer.parseInt(userId));
        } else if (!StringUtils.isEmpty(userId) && !StringUtils.isNumeric(userId)) {
            marketAddress.setUserId(0);
        }
        marketAddress.setName(name);
        CommonData<MarketAddress> data = userService.queryAddress(marketAddress, info);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("collect/list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo collectList(String userId, String valueId, BasePageInfo info) {
        MarketCollect marketCollect = new MarketCollect();

        if (!StringUtils.isNumeric(userId) && !StringUtils.isEmpty(userId)) {
            marketCollect.setUserId(0);
        } else if (StringUtils.isNumeric(userId) && !StringUtils.isEmpty(userId)){
            marketCollect.setUserId(Integer.parseInt(userId));
        }
        if (!StringUtils.isNumeric(valueId) && !StringUtils.isEmpty(valueId)) {
            marketCollect.setValueId(0);
        } else if (StringUtils.isNumeric(valueId) && !StringUtils.isEmpty(valueId)){
            marketCollect.setValueId(Integer.parseInt(valueId));
        }

        CommonData<MarketCollect> data = userService.queryCollect(marketCollect, info);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("footprint/list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo printList(String userId, String goodsId, BasePageInfo info) {
        MarketFootprint marketFootprint = new MarketFootprint();

        if (StringUtils.isNumeric(userId) && !StringUtils.isEmpty(userId)) {
            marketFootprint.setUserId(Integer.parseInt(userId));
        } else if (!StringUtils.isNumeric(userId) && !StringUtils.isEmpty(userId)){
            marketFootprint.setUserId(0);
        }
        if (StringUtils.isNumeric(goodsId) && !StringUtils.isEmpty(goodsId)) {
            marketFootprint.setGoodsId(Integer.parseInt(goodsId));
        } else if (!StringUtils.isNumeric(goodsId) && !StringUtils.isEmpty(goodsId)){
            marketFootprint.setGoodsId(0);
        }

        CommonData<MarketFootprint> data = userService.queryFootprint(marketFootprint, info);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("history/list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo historyList(String userId, String keyword, BasePageInfo info) {

        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();

        if (StringUtils.isNumeric(userId) && !StringUtils.isEmpty(userId)) {
            marketSearchHistory.setUserId(Integer.parseInt(userId));
        } else if (!StringUtils.isNumeric(userId) && !StringUtils.isEmpty(userId)){
            marketSearchHistory.setUserId(0);
        }

        marketSearchHistory.setKeyword(keyword);
        CommonData<MarketSearchHistory> data = userService.queryHistory(marketSearchHistory, info);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("feedback/list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo feedbackList(String username, String id, BasePageInfo info) {

        MarketFeedback marketFeedback = new MarketFeedback();

        if (StringUtils.isNumeric(id) && !StringUtils.isEmpty(id)) {
            marketFeedback.setId(Integer.parseInt(id));
        } else if (!StringUtils.isNumeric(id) && !StringUtils.isEmpty(id)){
            marketFeedback.setId(0);
        }

        marketFeedback.setUsername(username);

        CommonData<MarketFeedback> data = userService.queryFeedback(marketFeedback, info);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

}
